package com.wwwarehouse.contract.core;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;

import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomDisViewPager;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.keyboard.VirtualKeyboardView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.contract.event.ContractEvent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import contract.wwwarehouse.com.contract.R;

/**
 * Created by feng.gao on 2017/9/20.
 */

public abstract class BaseViewPagerFragment extends BaseFragment implements ElasticScrollView.OnPullListener {
	protected CustomDisViewPager mCustomPager = null;
	private CirclePageIndicator mPagerInternal = null;
	protected RelativeLayout mRlBkg = null;
	protected StateLayout mStateLayout = null;
	protected ElasticScrollView mCustomScroll;
	protected Button mConfirmBtn;
	protected VirtualKeyboardView mKeyBoard;

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
			EventManager.register(this);
		}
		return inflater.inflate(R.layout.contract_fred_base_viewpager_fragment, container, false);
	}

	@Override
	public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		mKeyBoard = (VirtualKeyboardView) view.findViewById(R.id.keyboard);
		mCustomPager = (CustomDisViewPager) view.findViewById(R.id.vp_content);
		mPagerInternal = (CirclePageIndicator) view.findViewById(R.id.pager_indicator);
		mRlBkg = (RelativeLayout) view.findViewById(R.id.rl_bkg);
		mStateLayout = (StateLayout) view.findViewById(R.id.gaofeng_sl_state);
		mCustomScroll = (ElasticScrollView) view.findViewById(R.id.ecl_custom);
		mConfirmBtn = (Button) view.findViewById(R.id.confirm_btn);
		mCustomScroll.setOnPullListener(this);
		mRlBkg.setVisibility(View.INVISIBLE);
		mConfirmBtn.setVisibility(View.INVISIBLE);
		mKeyBoard.setOnConfirmListener(new VirtualKeyboardView.OnConfirmListener() {
			@Override
			public void confirm() {
				if (mKeyBoard.getVisibility() == View.VISIBLE) {
					mKeyBoard.setVisibility(View.GONE);
				}
			}
		});
		mKeyBoard.setVisibility(View.GONE);

	}


	public void sendRequest(String url, Map map, final int what) {
		mStateLayout.showProgressView(true);
		NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
			@Override
			public void onSucceed(CommonClass commonClass, int i) {
				if ("0".equals(commonClass.getCode())) {
					mRlBkg.setVisibility(View.VISIBLE);
					if (commonClass.getData() != null) {
						mStateLayout.showContentView();
						onResponse(commonClass.getData().toString(), what);
					} else {
						mStateLayout.showEmptyView(true);
						mStateLayout.setEmptyListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								requestDatas();
							}
						});
					}
				} else {
					mStateLayout.showSystemView(commonClass.getMsg() , true);
					mStateLayout.setSystemListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							requestDatas();
						}
					});
					onResponseFail(commonClass.getMsg(), what);
				}

			}

			@Override
			public void onFailed(String s, int i) {
				mStateLayout.showSystemView(s , true);
				mStateLayout.setSystemListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}

			@Override
			public void onFinish(int i) {

			}
		}, what);
	}

	public void sendRequest(String url, Map map, final int what , final boolean errorToast) {
		showProgressDialog("请求中");
		NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
			@Override
			public void onSucceed(CommonClass commonClass, int i) {
				dismissProgressDialog();
				if ("0".equals(commonClass.getCode())) {
					mRlBkg.setVisibility(View.VISIBLE);
					mStateLayout.showContentView();
					if (commonClass.getData() != null) {
						onResponse(commonClass.getData().toString(), what);
					} else {
						if (errorToast) {
							onResponse("" , what);
							mStateLayout.showContentView();
							return;
						}
					}
				} else {
					mStateLayout.showSystemView(commonClass.getMsg() , true);
					mStateLayout.setSystemListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							requestDatas();
						}
					});
					onResponseFail(commonClass.getMsg(), what);
				}

			}

			@Override
			public void onFailed(String s, int i) {
				dismissProgressDialog();

				mStateLayout.showSystemView(s , true);
				mStateLayout.setSystemListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}

			@Override
			public void onFinish(int i) {
				dismissProgressDialog();


			}
		}, what);
	}


	public void sendProgressRequest(String url, Map map, final int what) {
		showProgressDialog("请求中……");
		NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
			@Override
			public void onSucceed(CommonClass commonClass, int i) {
				dismissProgressDialog();
				if ("0".equals(commonClass.getCode())) {
//					mRlBkg.setVisibility(View.VISIBLE);
					if (commonClass.getData() != null)
					onResponse(commonClass.getData().toString(), what);
				} else {
					onResponseFail(commonClass.getMsg(), what);
				}

			}

			@Override
			public void onFailed(String s, int i) {
				mStateLayout.showSystemView(s , true);
				mStateLayout.setSystemListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}

			@Override
			public void onFinish(int i) {

			}
		}, what);
	}

	public void sendSientRequest(String url, Map map, final int what) {
		NoHttpUtils.httpPost(url, map, new NoHttpUtils.OnResponseListener() {
			@Override
			public void onSucceed(CommonClass commonClass, int i) {
				dismissProgressDialog();
				if ("0".equals(commonClass.getCode())) {
//					mRlBkg.setVisibility(View.VISIBLE);
					if (commonClass.getData() != null)
						onResponse(commonClass.getData().toString(), what);
				} else {
					showProgressDialog(commonClass.getMsg());
					onResponseFail(commonClass.getMsg(), what);
				}

			}

			@Override
			public void onFailed(String s, int i) {
				mStateLayout.showSystemView(s , true);
				mStateLayout.setSystemListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						requestDatas();
					}
				});
			}

			@Override
			public void onFinish(int i) {

			}
		}, what);
	}
	public abstract void onResponse(String response, int what);

	protected   void onResponseFail(String response, int what) {

	}

	protected void setData(Serializable mData , int total , int pageNum , String className , boolean isFunction) {
		if (mData != null) {
			List<Fragment> list = new ArrayList<>();
				int mPager;
				if (total % pageNum == 0) {
					mPager = total / pageNum;
				} else {
					mPager = (int) (total / pageNum) + 1;
				}
				for (int i = 0; i < mPager; i++) {
					Fragment fragment = Fragment.instantiate(mActivity, className);
					Bundle bundle = new Bundle();
					bundle.putSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA, mData);
					bundle.putInt(ContractConstant.KEY_BUNDLE_POSITION, i);
					bundle.putBoolean(ContractConstant.KEY_IS_FUNCTION , isFunction);
					bundle.putInt(ContractConstant.KEY_BUNDLE_PAGENUM , pageNum);
					fragment.setArguments(bundle);
					list.add(fragment);
				}
				if (isAdded()) {
					MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter
							(getChildFragmentManager(), list);
					mCustomPager.setAdapter(pagerAdapter);
					mPagerInternal.setViewPager(mCustomPager);
					mStateLayout.showContentView();
					mRlBkg.setVisibility(View.VISIBLE);
				}


			}
		}

	protected void setData(Serializable mData , int total , int pageNum , String className ) {
		if (mData != null) {
			List<Fragment> list = new ArrayList<>();
			int mPager;
			if (total % pageNum == 0) {
				mPager = total / pageNum;
			} else {
				mPager = (int) (total / pageNum) + 1;
			}
			for (int i = 0; i < mPager; i++) {
				Fragment fragment = Fragment.instantiate(mActivity, className);
				Bundle bundle = new Bundle();
				bundle.putSerializable(ContractConstant.KEY_BUNDLE_VIEW_PAGER_DATA, mData);
				bundle.putInt(ContractConstant.KEY_BUNDLE_POSITION, i);
				bundle.putInt(ContractConstant.KEY_BUNDLE_PAGENUM , pageNum);
				fragment.setArguments(bundle);
				list.add(fragment);
			}
			if (isAdded()) {
				MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter
						(getChildFragmentManager(), list);
				mCustomPager.setAdapter(pagerAdapter);
				mPagerInternal.setViewPager(mCustomPager);
				mStateLayout.showContentView();
				mRlBkg.setVisibility(View.VISIBLE);
			}


		}
	}

	@Override
	public void onDownPull() {

	}

	@Override
	public void onUpPull() {

	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (this.getClass().isAnnotationPresent(BindEventBus.class)) {
			EventManager.unregister(this);
		}
	}

	public void onEventMainThread(ContractEvent contractEvent) {
		if (contractEvent != null) {
			receiveEvent(contractEvent);
		}

	}

	protected void receiveEvent(ContractEvent contractEvent) {

	}



}
